home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / pascal / tdlib20.zip / PROJECT3.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-03  |  8KB  |  163 lines

  1. (******************************************************************************
  2. *                                  project3                                   *
  3. ******************************************************************************)
  4. unit project3;
  5.  
  6. (*******************************************************************************
  7. *      this unit has 2 instances : the first one is used with the windows      *
  8. *               graphic user interface, and the other does not,                *
  9. *                                                                              *
  10. *             +-------------------------------------------------+              *
  11. *             |  this unit is NOT interfaced to the window GUI, |              *
  12. *             |   for use on a bare screen ONLY - for runTime!  |              *
  13. *             |                ****                             |              *
  14. *             +-------------------------------------------------+              *
  15. *                                                                              *
  16. *this unit handles the 3d -> 2d projections, we use 2 different methods        *
  17. *       of projections :                                                       *
  18. *                                                                              *
  19. *               A : axonometric projections, no perspective due to             *
  20. *                       distance is performed, the general way                 *
  21. *                       we can look at the coordinate system is as             *
  22. *                       follows :                                              *
  23. *                                                                              *
  24. *                               |  z axis                                      *
  25. *                               |                                              *
  26. *                              / \                                             *
  27. *                    x axis   /   \  y axis                                    *
  28. *                                                                              *
  29. *               B : perspective projections : the normal eye perspective       *
  30. *                       projection is performed, we can look at the 3d         *
  31. *                       universe we are refering to as a cube of               *
  32. *                       1000 x 1000 x 1000 integer locations, with             *
  33. *                       the x axis, and y axis parallel to the screen          *
  34. *                       x, y axis respectivly, and the z axis going into       *
  35. *                       the screen.                                            *
  36. *                                                                              *
  37. *                       we will look at the coordinate system as follows :     *
  38. *                                                                              *
  39. *                       │ Y axis                                               *
  40. *                       │                                                      *
  41. *                Z axis x------ X axis                                         *
  42. *                                                                              *
  43. *                                                                              *
  44. *******************************************************************************)
  45.  
  46. interface
  47.  
  48. uses  
  49. {$ifndef windows}  
  50.    graph,
  51. {$endif}
  52.    hdr3d
  53.    ;
  54.  
  55. const
  56.         perspective     : boolean = false; 
  57.         {true = perspective, else = axonometric}
  58. {$ifndef windows}
  59. var
  60.     MaxX, MaxY : word;          { In pixels for graphics screen }
  61.     MaxColor   : word;
  62.     GraphDriver           : integer;
  63.     GraphMode             : integer;
  64. {$endif}
  65.  
  66. procedure calcPoint(p3d : point3d; var psc : screenPoints);
  67. procedure setPerspective;
  68. procedure resetPerspective;
  69. procedure togglePerspective;
  70.  
  71. implementation
  72.  
  73. {$ifndef windows}
  74. var 
  75.    oldExitProc           : pointer;
  76.  
  77. (******************************************************************************
  78. *                                 MyExitProc                                  *
  79. ******************************************************************************)
  80. procedure MyExitProc; far;
  81. Begin
  82.   ExitProc := OldExitProc; { Restore exit procedure address }
  83.   CloseGraph;              { Shut down the graphics system }
  84. End; { MyExitProc }
  85.  
  86. (*******************************************************************************
  87. *                                  StartGraph                                  *
  88. *******************************************************************************)
  89. Procedure StartGraph;
  90. var
  91.   ErrorCode : integer;
  92. Begin
  93.   { when using Crt and graphics, turn off Crt's memory-mapped writes }
  94.   OldExitProc := ExitProc;                { save previous exit proc }
  95.   ExitProc := @MyExitProc;                { insert our exit proc in chain }
  96.   GraphDriver := Detect;                  { use autodetection }
  97.   InitGraph(GraphDriver, GraphMode, '');  { activate graphics }
  98.   ErrorCode := GraphResult;               { error? }
  99.   if ErrorCode <> grOk then
  100.   Begin
  101.     Writeln('Graphics error: ', GraphErrorMsg(ErrorCode));
  102.     Halt(1);
  103.   End;
  104.   MaxColor := GetMaxColor;  { Get the maximum allowable drawing color }
  105.   MaxX := GetMaxX;          { Get screen resolution values }
  106.   MaxY := GetMaxY;
  107. End; { Initialize }
  108.  
  109. {$endif}
  110.  
  111. (******************************************************************************
  112. *                                  CalcPoint                                  *
  113. ******************************************************************************)
  114. procedure CalcPoint;
  115.  {Calculate screen cordinates of 3d location into screen}
  116.  
  117. begin with p3d, psc do begin
  118.      if Perspective then begin
  119.           z  := z + HalfWidth;
  120.           if z < 0 then 
  121.             z := 0;
  122.           sX := round((x * z / HalfWidth + HalfWidth) * (MaxX / ScreenWidth));
  123.           sY := round((HalfWidth - y * z / HalfWidth) * (MaxY / ScreenWidth));
  124.      end else begin
  125.           sX := round((HalfWidth + cosine_x * x - cosine_y * y) * (MaxX / ScreenWidth));
  126.           sY := round((HalfWidth - z + sine_x * x + sine_y * y) * (MaxY / ScreenWidth));
  127.      end;
  128.    end; {with}
  129. end; { calcPoint }
  130.  
  131. (******************************************************************************
  132. *                               setPerspective                                *
  133. ******************************************************************************)
  134. procedure setPerspective;
  135. begin
  136.         perspective := true;
  137. end; {setPerspective}
  138.  
  139. (******************************************************************************
  140. *                              resetPerspective                               *
  141. ******************************************************************************)
  142. procedure resetPerspective;
  143. begin
  144.         perspective := false;
  145. end; {resetPerspective}
  146.  
  147. (******************************************************************************
  148. *                              togglePerspective                              *
  149. ******************************************************************************)
  150. procedure togglePerspective;
  151. begin
  152.         perspective := not(perspective);
  153. end; {togglePerspective}
  154.  
  155. (******************************************************************************
  156. *                                    end.                                     *
  157. ******************************************************************************)
  158. {$ifndef windows}
  159. begin
  160.     startGraph;
  161. {$endif}
  162. end.
  163.